
//-----------------MATRICES---------------
float4x4 xWorld: World;				
float4x4 xView: View;
float4x4 xProjection: Projection;

//-----------------TEXTURES---------------

Texture xTexture;
sampler ColoredTextureSampler = sampler_state { texture = <xTexture> ;
magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};


//-----------------CONSTANTS----------------

float4 xLightPosition;
float xTime;

float Ka;
float Kd;
float Ks;
float A;
float4 xAmbientLight;
float3 xCamera;
float3 xLightDirection;
float4 xLColor;

float xSpecularFactor;
float xSpecularPower;
float4 xSpecularColor;



//------------------- STRUCTS -------------------   

struct BufferToVertex
{
	float3 Pos : POSITION;
	float4 Normal : NORMAL;
	float4 UV : TEXCOORD0;
};


struct VertexToPixel
{
	float4 Pos:POSITION;
	float4 UV:TEXCOORD0;
	float4 Pos3D:TEXCOORD1;
	float4 Normal:TEXCOORD2;
};


struct PixelToFrame
{
	float4 Color:COLOR;
};

//---------------------- SHADERS ------------------------

VertexToPixel texturedVS(BufferToVertex IN)
{
	VertexToPixel OUT = (VertexToPixel)0;
	float4x4 WorldViewProj;

	WorldViewProj=mul(xWorld,xView);
	WorldViewProj=mul(WorldViewProj,xProjection);

	OUT.Pos=mul(float4(IN.Pos.xyz,1.0), WorldViewProj);


	OUT.Pos3D=mul(float4(IN.Pos.xyz,1.0),xWorld);  
	OUT.Normal=IN.Normal;
	float2 uv = IN.UV.xy;  
	OUT.UV=float4(uv,1.0,1.0);

	return OUT;



	return OUT;
}


PixelToFrame texturedPS(VertexToPixel IN) 
{
	PixelToFrame OUT = (PixelToFrame)0;
	IN.Normal = normalize ( IN.Normal) ;
	
	float3 N = normalize(IN.Normal.xyz);   
	float3 L = normalize(xLightPosition.xyz-IN.Pos3D.xyz);  
	float3 V2 = normalize(xCamera.xyz - IN.Pos3D.xyz);
	float3 R2 = normalize(reflect(-L, N));
	float3 H = normalize( L +  V2);
	//
	float4 Ia = Ka * xAmbientLight;
	float3 IsB = Ks * pow( saturate(dot(N,H)), A );
	float3 Id = Kd * saturate( dot(N,L) );

	float4 LightColor = float4(1.0f, 1.0f, 1.0f, 1.0f);

	OUT.Color = Ia + (Id.x+IsB.x) *LightColor;
	return OUT;

}




// ------------ TECHNIQUES --------------

technique textured{
	pass p0 {
		VertexShader = compile vs_3_0 texturedVS();
		PixelShader = compile ps_3_0 texturedPS();
	}
}


